package com.cn.cs;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import static java.lang.Class.forName;
//统计
public class work2 {
        public static void main(String[] args)  {
            try {
                forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql1","root","root");
                //获取操作数据库的对象
                PreparedStatement pst =con.prepareStatement("SELECT id,class,COUNT(*) as 人数 from student group by class");
                //执行sql获取结果数据
                ResultSet rs =pst.executeQuery();
                //遍历结果集
                while(rs.next()){
                    System.out.println("\t班级:"+rs.getInt("class")+"\t人数:"+rs.getInt("人数"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
